﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Didsoft.SalesLive.Entidades;

namespace Didsoft.SalesLive.UI.Views.VistaModelo
{
    public class VmHome : ViewModelBase<Persona>
    {
        Didsoft.SalesLive.Negocios.PersonaNegocios contexto = new Negocios.PersonaNegocios();

        public VmHome()
        {           

            CargarPersonas();
            SetDomainContext(new Didsoft.SalesLive.Negocios.PersonaNegocios());
            //CmdSave.CommandExecuted += new EventHandler<EventArgs>(CmdSave_CommandExecuted);
        }

        void CmdSave_CommandExecuted(object sender, EventArgs e)
        {
            //contexto.Personas.Add(SelectedItem);
            contexto.SubmitChanges(guardado =>
                                   {
                                       if (guardado.HasError)
                                       {
                                           MessageBox.Show(guardado.Error.Message);
                                           guardado.MarkErrorAsHandled();
                                       }

                                   }, null);
            CargarPersonas();
            
            
        }

     
        private void CargarPersonas()
        {
            contexto.Load<Persona>(contexto.GetPersonasQuery(),
                                loadoperation =>
                                {
                                    ItemsSource = loadoperation.Entities.ToObservableCollection();
                                }, null);

        }


        void guardado(Action<System.ServiceModel.DomainServices.Client.SubmitOperation> resultado)
        { 
           
        }
    }


    public static class Extension
    {
        public static System.Collections.ObjectModel.ObservableCollection<T> ToObservableCollection<T>(this System.Collections.Generic.IEnumerable<T> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            return new System.Collections.ObjectModel.ObservableCollection<T>(source);
        }
    }

    public static class OperationExtensions
    {
        public static System.Threading.Tasks.Task<T> AsTask<T>(this T operation)
          where T : System.ServiceModel.DomainServices.Client.OperationBase
        {
            System.Threading.Tasks.TaskCompletionSource<T> tcs =
              new System.Threading.Tasks.TaskCompletionSource<T>(operation.UserState);

            operation.Completed += (sender, e) =>
            {
                if (operation.HasError && !operation.IsErrorHandled)
                {
                    tcs.TrySetException(operation.Error);
                    operation.MarkErrorAsHandled();
                }
                else if (operation.IsCanceled)
                {
                    tcs.TrySetCanceled();
                }
                else
                {
                    tcs.TrySetResult(operation);
                }
            };

            return tcs.Task;
        }
    }
}
